//
//  ViewController.m
//  7-9点餐系统
//
//  Created by 园平 on 7/9/16.
//  Copyright © 2016 吴园平. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *fruitLable;
@property (weak, nonatomic) IBOutlet UILabel *mainLable;
@property (weak, nonatomic) IBOutlet UILabel *drinkLable;

@property (weak, nonatomic) IBOutlet UIPickerView *pickView;

@property (nonatomic,strong) NSArray *foods;


@end

@implementation ViewController
/**
 *  懒加载：即重写getter方法
 */

- (NSArray *)foods{
    if (_foods == nil) {
        //获取全路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"foods.plist" ofType:nil];
        //加载数据
        _foods = [NSArray arrayWithContentsOfFile:path];
    }
    return _foods;

}

- (IBAction)random:(id)sender {
    
    
    for (int i = 0; i < 3 ; i++) {
        
        // 假设让第0列随机选中一行
        // 取出第0列的行数
        NSInteger count = [self.foods[i] count];
        
        int random = arc4random_uniform((u_int32_t)count);
        // 不会触发代理的选中方法
        [self.pickView selectRow:random inComponent:i animated:YES];
        
        // 主动给label赋值
        [self pickerView:_pickView didSelectRow:random inComponent:i];
    }

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //也可拖线设置数据源和代理
    self.pickView.dataSource = self;
    self.pickView.delegate = self;
    
    for (int i = 0; i < 3; i++) {
        [self pickerView:_pickView didSelectRow:0 inComponent:i];
        
    }
}



#pragma mark - <UIPickerViewDelegate>

//返回第component列第row行的标题

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    return self.foods[component][row];
}



#pragma mark - <UIPickerViewDataSource>
//返回pickView有多少列
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return self.foods.count;

}

//返回第component列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    return [self.foods[component] count];

}
//返回每列的高度

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    
    return 45;
}


//自定义方法：
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    switch (component) {
        case 0:
            //设置水果
            _fruitLable.text = self.foods[component][row];
            
            break;
        case 1:
            //设置主食
             _mainLable.text = self.foods[component][row];
            
            break;
        case 2:
            //设置饮料
            _drinkLable.text = self.foods[component][row];
            
            break;
         
    }
}

@end
